package me.zhengjie.controller.store;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.StoreLocalDao;
import me.zhengjie.dao.dto.StoreLocalQueryCriteria;
import me.zhengjie.excel.DataHandle;
import me.zhengjie.server.StoreLocalService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.vo.Result;
import me.zhengjie.vo.task.TaskViewVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequiredArgsConstructor
@Api(tags = "StoreController")
@RequestMapping("/api/storeLocal")
public class StoreController {
    private static final Logger inboundLog = LoggerFactory.getLogger("store");

    @Resource
    private StoreLocalService storeLocalService;


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('storeLocal:list')")
    public void exportStoreLocal(HttpServletResponse response, StoreLocalQueryCriteria criteria) throws IOException {
        inboundLog.info("导出数据");
        storeLocalService.download(storeLocalService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询StoreController")
    @PreAuthorize("@el.check('storeLocal:list')")
    public ResponseEntity<PageResult<StoreLocalDao>> queryStoreLocal(StoreLocalQueryCriteria criteria) {
        inboundLog.info("查询StoreController");
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(storeLocalService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增StoreController")
    @ApiOperation("新增StoreController")
    @PreAuthorize("@el.check('storeLocal:add')")
    public ResponseEntity<Object> createStoreLocal(@Validated @RequestBody StoreLocalDao resources) {
        inboundLog.info("新增StoreController");
        storeLocalService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改StoreController")
    @ApiOperation("修改StoreController")
    @PreAuthorize("@el.check('storeLocal:edit')")
    public ResponseEntity<Object> updateStoreLocal(@Validated @RequestBody StoreLocalDao resources) {
        inboundLog.info("修改StoreController");
        storeLocalService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除StoreController")
    @ApiOperation("删除StoreController")
    @PreAuthorize("@el.check('storeLocal:del')")
    public ResponseEntity<Object> deleteStoreLocal(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        inboundLog.info("删除StoreController");
        storeLocalService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @RequestMapping("lockStore")
    public Result<?> lockStore(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids) {
        return storeLocalService.lockStore(ids);
    }

    @RequestMapping("unlockStore")
    public Result<?> unlockStore(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids){
        return storeLocalService.unlockStore(ids);
    }
    @RequestMapping("releaseStore")
    public Result<?> releaseStore(@ApiParam(value = "传ID数组[]") @RequestBody List<String> ids){
        return storeLocalService.releaseStore(ids);
    }

    @RequestMapping("updateWareType")
    public Result<?> updateWareType(@RequestBody Map<String, Object> params){
        if (params == null || params.get("wareType") == null || params.get("storeList") == null){
            return Result.error("参数错误");
        }
        if (!(params.get("wareType") instanceof String)){
            return Result.error("参数错误");
        }
        if (!(params.get("storeList") instanceof List)){
            return Result.error("参数错误");
        }
        return storeLocalService.updateWareType(params);
    }

    @RequestMapping("importStoreExcel")
    public Result<?> importStoreExcel(String filePath) {
        // String filePath = "D:\\大欣储位导入表.xlsx";
        List<StoreLocalDao> storageList = new ArrayList<>();
        DataHandle.HandleStoreData(filePath, storageList);
        boolean result = storeLocalService.saveBatch(storageList);

        if (result) {
            return new Result<>(200, "导入成功", null);
        }
        return new Result<>(500, "导入失败", null);
    }
}
